const Base = require('./base')
module.exports = class extends Base {
  async instance (id) {
    const account = await this.get_authorizer_by_id(id)
    if (!account.authorized) return false
    this.appid = account.appid
    this.access_token = await this.get_access_token(this.appid)
    return this
  }
  /**
     * add 添加客服帐号 kf_account nickname password
     * update 修改客服帐号
     * del 删除客服帐号
     * uploadheadimg 设置客服帐号的头像 kf_account = kf_account
     * getkflist 获取所有客服账号
     * @param key
     * @returns {string}
     */
  kf (key, post, params = {}) {
    let url = `https://api.weixin.qq.com/customservice/kfaccount/${key}?access_token=${this.access_token}`
    if (Object.keys(params).length > 0) {
      const key = Object.keys(params)[0]
      url = `${url}&${key}=${url[key]}`
    }
    return yy.http.post(url, post)
  }

  /**
     * 发送客服消息
     * {"touser":"OPENID","msgtype":"text","text":{"content":"Hello World"}}
     * {"touser":"OPENID","msgtype":"image","image":{"media_id":media_id}}
     * {"touser":"OPENID","msgtype":"video","video":{"media_id":"MEDIA_ID","thumb_media_id":"MEDIA_ID","title":"TITLE","description":"DESCRIPTION"}}
     * {"touser":"OPENID","msgtype":"voice","voice":{"media_id":"MEDIA_ID"}}
     * {"touser":"OPENID","msgtype":"music","music":{"title":"MUSIC_TITLE","description":"MUSIC_DESCRIPTION","musicurl":"MUSIC_URL","hqmusicurl":"HQ_MUSIC_URL","thumb_media_id":"THUMB_MEDIA_ID" }}
     * {"touser":"OPENID","msgtype":"news","news":{ "articles": [{"title":"Happy Day","description":"Is Really A Happy Day","url":"URL","picurl":"PIC_URL"}]}}
     * {"touser":"OPENID","msgtype":"mpnews","mpnews":{"media_id":"MEDIA_ID"}}
     * {"touser":"OPENID","msgtype":"wxcard","wxcard":{ "card_id":"123dsdajkasd231jhksad"}}
     * ... 关联客服信息在后面增加 "customservice":{"kf_account": "test1@kftest"} 字段
     * @param post
     * @returns {*}
     */
  send (post) {
    const url = `https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=${this.access_token}`
    return yy.http.post(url, post)
  }
}
